package com.wcq.studentbackend.controller;

import com.wcq.studentbackend.common.R;
import com.wcq.studentbackend.dto.GradeDTO;
import com.wcq.studentbackend.dto.StudentDetailDTO;
import com.wcq.studentbackend.service.GradeService;
import com.wcq.studentbackend.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/student") // 注意这里的路径是 /api/student (单数)
@Api(value = "学生信息管理")
public class MyInfoController {


    @Autowired
    private GradeService gradeService;

    @Autowired
    private StudentService studentService;

    @GetMapping("/my-grades")
    @ApiOperation(value = "获取当前学生的成绩信息接口")
    public R<List<GradeDTO>> getMyGrades(Authentication authentication) {
        log.info("Attempting to access /my-grades with authentication: {}", authentication);
        if (authentication == null || !authentication.isAuthenticated()) {
            log.warn("Attempt to access /my-grades without authentication.");
            return R.error("用户未认证");
        }
        String stuNo = authentication.getName(); // Spring Security 通常用 username 作为 name
        log.info("Fetching grades for student stuNo: {}", stuNo);

        // 调用已有的 GradeService 方法
        List<GradeDTO> grades = gradeService.getGradesByStudentStuNo(stuNo);

        if (grades == null) { // GradeService 实现中已经处理了返回 Collections.emptyList() 的情况
            log.warn("Grade service returned null unexpectedly for stuNo: {}, returning empty list.", stuNo);
            return R.success(Collections.emptyList());
        }

        if (grades.isEmpty()) {
            log.info("No grades found for student stuNo: {}. Returning empty list.", stuNo);
        }
        log.info("Found {} grades for student stuNo: {}", grades.size(), stuNo);
        return R.success(grades);
    }

    @GetMapping("/my-profile")
    @ApiOperation(value = "获取当前学生的个人信息接口")
    public R<StudentDetailDTO> getMyProfile(Authentication authentication) {
        log.info("Attempting to access /my-profile with authentication: {}", authentication);
        if (authentication == null || !authentication.isAuthenticated()) {
            log.warn("Attempt to access /my-profile without authentication.");
            return R.error("用户未认证");
        }
        String stuNo = authentication.getName();
        log.info("Fetching profile for student stuNo: {}", stuNo);

        StudentDetailDTO studentDetail = studentService.getStudentDetailByStuNo(stuNo);

        if (studentDetail == null) {
            log.warn("Student profile not found for stuNo: {}", stuNo);
            return R.error("未能获取到学生信息");
        }
        return R.success(studentDetail);
    }

    // 未来可以添加更多获取当前学生信息的接口，例如：
    // @GetMapping("/my-profile")
    // public R<StudentDTO> getMyProfile(Authentication authentication) { ... }
} 